package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.Address;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqComment;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqNoticeComment;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.domain.vo.WqCommentVO;
import com.ruoyi.wq.mapper.WqCommentMapper;
import com.ruoyi.wq.mapper.WqDynamicMapper;
import com.ruoyi.wq.mapper.WqNoticeCommentMapper;
import com.ruoyi.wq.service.IWqCommentReplyService;
import com.ruoyi.wq.service.IWqCommentService;
import com.ruoyi.wq.service.IWqNoticeCommentService;
import com.ruoyi.wq.service.IWqNoticeGiveService;

/**
 * 动态评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@Service
public class WqCommentServiceImpl extends ServiceImpl<WqCommentMapper, WqComment> implements IWqCommentService {
	
    @Autowired
    private WqCommentMapper wqCommentMapper;
    
    @Autowired
    private IWqCommentReplyService wqCommentReplyService;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private WqDynamicMapper wqDynamicMapper;
    
    @Autowired
    private WqNoticeCommentMapper wqNoticeCommentMapper;
    
    @Autowired
    private IWqNoticeGiveService wqNoticeGiveService;
    
    @Autowired
    private IWqNoticeCommentService wqNoticeCommentService;

    /**
     * 查询动态评论
     * 
     * @param id 动态评论主键
     * @return 动态评论
     */
    @Override
    public WqComment selectWqCommentById(String id)
    {
        return wqCommentMapper.selectWqCommentById(id);
    }

    /**
     * 查询动态评论列表
     * 
     * @param wqComment 动态评论
     * @return 动态评论
     */
    @Override
    public List<WqComment> selectWqCommentList(WqComment wqComment)
    {
        return wqCommentMapper.selectWqCommentList(wqComment);
    }

    /**
     * 新增动态评论
     * 
     * @param wqComment 动态评论
     * @return 结果
     */
    @Override
    public Boolean insertWqComment(WqComment wqComment){
    	Long userId = SecurityUtils.getUserId();
    	SysUser sysUser = sysUserService.getById(userId);
    	Address address = AddressUtils.getAddressByIP();
    	wqComment.setUserId(sysUser.getUserId());
    	wqComment.setAvatar(sysUser.getAvatar());
    	wqComment.setNickName(sysUser.getNickName());
    	wqComment.setSex(sysUser.getSex());
    	wqComment.setPhone(sysUser.getPhonenumber());
    	String content = wqComment.getContent();
    	if(StringUtils.getMatch(content)){
    		throw new ServiceException("请文明交友，多次警告会被关进小黑屋哦");
    	}
    	wqComment.setAge(sysUser.getAge());
    	wqComment.setLon(address.getLng());
    	wqComment.setLat(address.getLat());
    	wqComment.setCity(address.getCity());
    	wqCommentMapper.insert(wqComment);
    	
    	// 更新评论数量
    	Integer commentNum = getCommentNum(wqComment.getDynamicId());
    	WqDynamic dynamic = wqDynamicMapper.selectById(wqComment.getDynamicId());
    	dynamic.setCommentNum(commentNum);
    	wqDynamicMapper.updateById(dynamic);
    	
    	// 评论通知
    	if(!dynamic.getUserId().equals(userId)){
    		WqNoticeComment notice = new WqNoticeComment();
        	notice.setDynamicId(dynamic.getId());
        	if(StringUtils.isNotBlank(dynamic.getImage())){
        		notice.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
        	notice.setDynamicUserId(dynamic.getUserId());
        	notice.setCommentId(wqComment.getId());
        	notice.setAvatar(sysUser.getAvatar());
        	notice.setNickName(sysUser.getNickName());
        	notice.setSex(sysUser.getSex());
        	notice.setContent(wqComment.getContent());
        	notice.setNoticeUserId(dynamic.getUserId());
        	wqNoticeCommentMapper.insert(notice);
        	
        	// 更新用户未读消息数量
        	SysUser user = sysUserService.getById(dynamic.getUserId());
        	Integer commentNewNum = wqNoticeCommentService.getUnReadNum(user.getUserId());
        	user.setCommentNewNum(commentNewNum);
        	sysUserService.updateById(user);
    	}
    	return true;
    }
    
    /**
     * 获取评论数量
     * @param dynamicId
     * @return
     */
    private Integer getCommentNum(String dynamicId){
    	QueryWrapper<WqComment> qw = new QueryWrapper<>();
    	WqComment entity = new WqComment();
    	entity.setDynamicId(dynamicId);
    	qw.setEntity(entity);
    	Integer commentNum = wqCommentMapper.selectCount(qw);
    	return commentNum;
    }

    /**
     * 修改动态评论
     * 
     * @param wqComment 动态评论
     * @return 结果
     */
    @Override
    public int updateWqComment(WqComment wqComment)
    {
        wqComment.setUpdateTime(DateUtils.getNowDate());
        return wqCommentMapper.updateWqComment(wqComment);
    }

    /**
     * 批量删除动态评论
     * 
     * @param ids 需要删除的动态评论主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentByIds(String[] ids)
    {
        return wqCommentMapper.deleteWqCommentByIds(ids);
    }

    /**
     * 删除动态评论信息
     * 
     * @param id 动态评论主键
     * @return 结果
     */
    @Override
    public int deleteWqCommentById(String id)
    {
        return wqCommentMapper.deleteWqCommentById(id);
    }

	@Override
	public TableDataInfo getWqCommentList(WqCommentVO wqComment) {
		List<WqCommentVO> commentVOList = new ArrayList<WqCommentVO>();
		
		String noticeCommentId = wqComment.getNoticeCommentId();
		String noticeGiveId = wqComment.getNoticeGiveId();
		PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        if(pageNum < 2){
        	// 第一页
        	if(StringUtils.isNotBlank(noticeCommentId)){
    			// 评论通知消息置顶
    			WqNoticeComment notice = wqNoticeCommentMapper.selectById(noticeCommentId);
    			WqComment comment = baseMapper.selectById(notice.getCommentId());
    			String commentReplyId = notice.getCommentReplyId();
    			WqCommentVO replyPage = wqCommentReplyService.getReplyListByCommentId(comment.getId(),commentReplyId);
    			WqCommentVO wqCommentVO = new WqCommentVO();
    			BeanUtils.copyProperties(comment, wqCommentVO);
    			wqCommentVO.setNoticeCommentId(noticeCommentId);
    			wqCommentVO.setReplyList(replyPage.getReplyList());
    			wqCommentVO.setTotal(replyPage.getTotal());
    			wqCommentVO.setTime(DateUtils.getTime(wqCommentVO.getCreateTime()));
    			commentVOList.add(wqCommentVO);
    		}
        	if(StringUtils.isNotBlank(noticeGiveId)){
        		// 点赞通知消息置顶
    			WqNoticeGive notice = wqNoticeGiveService.getById(noticeGiveId);
    			if(StringUtils.isNotBlank(notice.getCommentId())){
    				WqComment comment = baseMapper.selectById(notice.getCommentId());
        			String commentReplyId = notice.getCommentReplyId();
        			WqCommentVO replyPage = wqCommentReplyService.getReplyListByCommentId(comment.getId(),commentReplyId);
        			WqCommentVO wqCommentVO = new WqCommentVO();
        			BeanUtils.copyProperties(comment, wqCommentVO);
        			wqCommentVO.setNoticeCommentId(noticeCommentId);
        			wqCommentVO.setReplyList(replyPage.getReplyList());
        			wqCommentVO.setTotal(replyPage.getTotal());
        			wqCommentVO.setTime(DateUtils.getTime(wqCommentVO.getCreateTime()));
        			commentVOList.add(wqCommentVO);
    			}
        	}
        }
		
		QueryWrapper<WqComment> qw = Condition.getQueryWrapper(wqComment);
		if(StringUtils.isNotBlank(noticeCommentId)){
			WqNoticeComment notice = wqNoticeCommentMapper.selectById(noticeCommentId);
			qw.ne("id", notice.getCommentId());
		}
		if(StringUtils.isNotBlank(noticeGiveId)){
			WqNoticeGive notice = wqNoticeGiveService.getById(noticeGiveId);
			if(StringUtils.isNotBlank(notice.getCommentId())){
				qw.ne("id", notice.getCommentId());
			}
		}
		qw.orderByDesc("create_time");
		IPage<WqComment> page = baseMapper.selectPage(Condition.getPage(wqComment),qw);
		
		WqDynamic dynamic = wqDynamicMapper.selectById(wqComment.getDynamicId());
		for(WqComment comment : page.getRecords()){
			WqCommentVO wqCommentVO = new WqCommentVO();
			BeanUtils.copyProperties(comment, wqCommentVO);
			
			/*if(dynamic.getStatus().equals("1")){
				if(!SecurityUtils.getUserId().equals(wqCommentVO.getUserId()) && !SecurityUtils.getUserId().equals(dynamic.getUserId())){
					wqCommentVO.setContent("只有作者和本人可见");
				}
			}*/
			
			if(!SecurityUtils.getUserId().equals(wqCommentVO.getUserId()) && !SecurityUtils.getUserId().equals(dynamic.getUserId())){
				wqCommentVO.setContent("只有作者和本人可见");
			}
			
			WqCommentVO replyPage = wqCommentReplyService.getReplyListByCommentId(comment.getId());
			wqCommentVO.setReplyList(replyPage.getReplyList());
			wqCommentVO.setTotal(replyPage.getTotal());
			wqCommentVO.setTime(DateUtils.getTime(wqCommentVO.getCreateTime()));
			commentVOList.add(wqCommentVO);
		}
		return Condition.getTableDataInfo(commentVOList, page.getTotal());
	}

	@Override
	public int deleteById(String id) {
		WqComment comment = baseMapper.selectById(id);
		if(!comment.getUserId().equals(SecurityUtils.getUserId())){
			throw new ServiceException("操作失败");
		}
		wqCommentReplyService.deleteByCommentId(id);
		return baseMapper.deleteById(id);
	}
}
